<?php

namespace App\Http\Controllers\Mobile\Personal;
use App\Models\Common\CollArea;
use App\Models\Personal\Favorites;
use Illuminate\Http\Request;
use App\Models\Common\CollOption;
use Illuminate\Support\Facades\DB;
use Style;

class FavoritesController extends CommonController{
    /**
     * 职位收藏夹
     * @param Request $request
     * @return mixed
     */
    public function favorites(Request $request){
        $user = auth()->guard('personal')->user();
        //关键词搜索
        $keyword  = trim($request->keyword);
        $job_status  = $request->job_status;
        //查询是否已投先觉条件
        if($job_status == '1'){
            $exFun="WhereExists";
        }else if($job_status == '2'){
            $exFun="WhereNotExists";
        }else{
            $exFun="Where";
        }

        $favorites = DB::table('favorites as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_job as t3', 't1.job_id', '=', 't3.id')
            //->leftjoin('resume_send as t4', 't1.job_id', '=', 't4.job_id')
            ->select(
                't1.id','t1.created_at','t1.com_id','t1.job_id',
                't2.company_name','t2.region',
                't3.job_title','t3.salary_above','t3.salary_below','t3.location1'
            )
            ->where('t1.uid',$user->uid)
            ->{$exFun}(function ($query) use ($job_status,$user)  {
                if($job_status != '') {
                    $query->select('status')->from('resume_send as t4')
                        ->whereRaw(env('DB_PREFIX')."t1.job_id = ".env('DB_PREFIX')."t4.job_id")
                        ->where('t4.personal_uid', $user->uid)
                        ->where('t4.status', '!=', 4);
                }
            })

            ->where(function ($query) use ($keyword) {
                if(strlen($keyword) > 0){
                    $query->where('t3.job_title', 'like', '%'.$keyword.'%')
                        ->orWhere('t2.company_name','like','%'.$keyword.'%');
                }
            })
            ->orderBy('created_at', 'desc')
            ->paginate(10);

        //dd($favorites);
        //处理数组
        foreach($favorites as $v){
            $v->company_name = str_limit($v->company_name,24);
            $v->job_title = str_limit($v->job_title,24);
            $v->region = str_limit(str_replace(',','',CollArea::getCityAddress($v->region)),20);
            $v->location1  = ($v->location1) ?  CollArea::getAreaName($v->location1) : '';
            $v->salary_above = ($v->salary_above) ? CollOption::getOptionName($v->salary_above,'expectedsalary') : '';
            $v->salary_below = ($v->salary_below) ? CollOption::getOptionName($v->salary_below,'expectedsalary') : '';
            $v->created_at = substr($v->created_at,0,10);
            $v->status  =DB::table('resume_send')->select('status')
                ->where("job_id",$v->job_id)
                ->where('personal_uid', $user->uid)
                ->where('status', '!=', 4)->count();
        }
        // dd($favorites);
        //分页分配参数 过滤boolean假值
        $appends =arrayFilter(array(
            'keyword' => $keyword,
            'job_status' =>  $job_status
        ));
        return Style::view('mobile.personal.favorites',compact('favorites','appends'));
    }

    /**
     * 投递简历
     * @return mixed
     */
    public function favoritesSend(){
        return Style::view('mobile.personal.favoritesSend');
    }
    /**
     * 单个职位收藏夹删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function favoritesCurrDel(Request $request){
        $user = auth()->guard('personal')->user();
        if(!empty($request->id)){
            $result = Favorites::where('id',$request->id)->where('uid',$user->uid)->delete();
            if($result){
                return response()->json(['status' => 200,'message'=>"职位收藏夹删除成功"]);
            }else{
                return response()->json(['status' => 404,'message'=>"职位收藏夹删除失败"]);
            }
        }else{
            return response()->json(['status' => 404,'message'=>"职位收藏夹删除失败"]);
        }
    }
}